package com.wtwd.device.service.impl;

import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.AppDetectionMapper;
import com.wtwd.device.model.entity.AppVersion;
import com.wtwd.device.model.vo.AppVersionVo;
import com.wtwd.device.model.vo.CheckAppVersionVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.AppDetectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * @author mjy
 * @date 2021/11/11
 * @description
 */
@Service
public class AppDetectionServiceImpl implements AppDetectionService {

    @Autowired
    private AppDetectionMapper appDetectionMapper;


    @Override
    public RespEntity checkAppVersion(String appName, Integer clientType, String appVersion) {
        AppVersionVo appVersionDto = searchMaxAppVersion(appName, clientType);
        if (appVersionDto == null) {
            return new RespEntity(RespCode.WEB_NOP_RECORD);
        }
        //构建前端返回对象
        CheckAppVersionVo checkAppVersionVo = new CheckAppVersionVo();
        //检测是否需要更新,需要更新则查看细节化更新类型
        if (appVersionDto.getVersion().compareTo(appVersion) > 0) {//检测到最新版本
            if (appVersionDto.getType().equals(2)) {//如果强提示升级
                if (appVersion.compareTo(appVersionDto.getLowestVersion()) >= 0) {//用户可以选择性更新
                    checkAppVersionVo.setFlag(2);
                } else {//否则需要强制更新
                    checkAppVersionVo.setFlag(1);
                }
            } else if (appVersionDto.getType().equals(3)) {//如果弱提示升级
                if (appVersion.compareTo(appVersionDto.getLowestVersion()) >= 0) {//用户可以选择性更新
                    checkAppVersionVo.setFlag(3);
                } else {//否则需要强制更新
                    checkAppVersionVo.setFlag(1);
                }
            } else if (appVersionDto.getType().equals(1)) {//强制升级
                checkAppVersionVo.setFlag(1);
            }
        } else {//不需要升级
            checkAppVersionVo.setFlag(0);
        }
        checkAppVersionVo.setCreateTime(appVersionDto.getCreateTime());
        checkAppVersionVo.setDescribe(appVersionDto.getDescribe());
        checkAppVersionVo.setInstallLocation(appVersionDto.getInstallLocation());
        checkAppVersionVo.setLowestVersion(appVersionDto.getLowestVersion());
        checkAppVersionVo.setVersion(appVersionDto.getVersion());
        return new RespEntity(RespCode.SUCCESS, checkAppVersionVo);
    }

    @Override
    public List<AppVersion> getAppVersionByConditions(AppVersion searchVersion) {
        return appDetectionMapper.getAppVersionByConditions(searchVersion);
    }

    /**
     * 根据app名称检测数据库中最新版本
     *
     * @param appName
     * @param clientType
     * @return
     */
    public AppVersionVo searchMaxAppVersion(String appName, Integer clientType) {
        //通过app名称，以及客户端类型获取，版本集合，寻找最高版本
        AppVersionVo appVersionVo = new AppVersionVo();
        List<AppVersionVo> appVersionVos = appDetectionMapper.getAppVersionListByNameAndClientType(appName, clientType);
        if (appVersionVos == null || appVersionVos.size() == 0) {
            return null;
        }
        appVersionVo = appVersionVos.stream().max(Comparator.comparing(AppVersionVo::getVersion)).get();
        return appVersionVo;
    }


}
